#define _CRT_SECURE_NO_WANINGS 1
#pragma warning(disable: 4996)
#pragma once
#include"common.h"
#include"threadcache.h"
#include"Concurrentalloc.hpp"
void Alloc1()
{
	for (int i = 0; i < 10; i++)
	{
		ConcurrentAlloc(7);
	}
}
void Alloc2()
{
	for (int i = 0; i < 5; i++)
	{
		ConcurrentAlloc(10);
	}
}
void TLStest()
{
	std::thread t1(Alloc1);
	t1.join();
	std::thread t2(Alloc2);
	t2.join();
}
void ConcurrentTest1()
{
	void* p1 = ConcurrentAlloc(5);
	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(3);
	void* p4 = ConcurrentAlloc(10);
	std::cout << p1 << ':' << p2 << ':' << p3 << ':' << p4 <<std::endl;
	cout << ((PAGE_ID)p1 >> PAGE_SHIFT) << endl;
}
void ConcurrentTest2()
{
	for (size_t i = 0; i < 1024; i++)
	{
		void* p1 = ConcurrentAlloc(6);
		std::cout << p1 << std::endl;
	}
	void* p2 = ConcurrentAlloc(8);
	std::cout << p2 << std::endl;
}
//void ConcurrentfreeTest1()
//{
//	void* p1 = ConcurrentAlloc(5);
//	void* p2 = ConcurrentAlloc(8);
//	void* p3 = ConcurrentAlloc(3);
//	void* p4 = ConcurrentAlloc(10);
//
//	ConcurrentFree(p1, 5);
//	ConcurrentFree(p2, 8);
//	ConcurrentFree(p3, 3);
//	ConcurrentFree(p4, 10);
//}
//void TestConcurrentAlloc1()
//{
//	void* p1 = ConcurrentAlloc(6);
//	void* p2 = ConcurrentAlloc(8);
//	void* p3 = ConcurrentAlloc(1);
//	void* p4 = ConcurrentAlloc(7);
//	void* p5 = ConcurrentAlloc(8);
//	void* p6 = ConcurrentAlloc(8);
//	void* p7 = ConcurrentAlloc(8);
//
//
//	cout << p1 << endl;
//	cout << p2 << endl;
//	cout << p3 << endl;
//	cout << p4 << endl;
//	cout << p5 << endl;
//
//	ConcurrentFree(p1, 6);
//	ConcurrentFree(p2, 8);
//	ConcurrentFree(p3, 1);
//	ConcurrentFree(p4, 7);
//	ConcurrentFree(p5, 8);
//	ConcurrentFree(p6, 8);
//	ConcurrentFree(p7, 8);
//}

void MultiThreadAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}

void MultiThreadAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(16);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}
void BigAlloc()
{
	void* p1 = ConcurrentAlloc(257 * 1024);
	ConcurrentFree(p1);

	void* p2 = ConcurrentAlloc(129 * 8 * 1024);
	ConcurrentFree(p2);
}
void TestMultiThread()
{
	std::thread t1(MultiThreadAlloc1);
	std::thread t2(MultiThreadAlloc2);

	t1.join();
	t2.join();
}
//int main()
//{
//	TestMultiThread();
//	return 0;
//}